import axios from 'axios';
import qs from 'qs';
import {MessageBox} from 'mint-ui';
import Vue from 'vue'
import router from '@/router';
import {Toast} from 'mint-ui';

// let userInfo = JSON.parse(window.localStorage.getItem('userInfo'));
// let token = userInfo?userInfo.token:'';

global.network = (function(){

  var getJson = function(url,param,successfn,errorfn,noneCode){

    axios.get(url,{
      params:param
    })
    .then((res)=>{
      // console.log(res);
      if(res.data.code==1000){
        console.log(param);
        console.log('必传参数缺失');
      }else if(res.data.code==200 || res.data.code==401){
        successfn(res.data);
      }
      else{
        console.log(url);
        console.log(res.data.code);
        Toast(res.data.message||res.data.code);
        if (noneCode) {
          noneCode(res.data.code);
        }
      }
    })
    .catch((error)=>{
      errorfn(error);
    });

  };

  var postJson = function(url,param,successfn,errorfn){

      return new Promise((resolve, reject) => {
          axios.post(url, qs.stringify(param)).then((res) => {
              resolve(res.data);
              console.log(res);
              if(res.data.code==1000){
                console.log('必传参数缺失');
              }else if(res.data.code==200 || res.data.code==401){
                successfn(res.data);
              }else{
                console.log(res.data.code);
                Toast(res.data.message);
              }
          }).catch((response) => {
              console.log('f', response);
              errorfn(response);
          })
      })

  };
  return {
    get:getJson,
    post:postJson
  }
})();


export{
  network
}
